package com.etalk.crm.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.etalk.crm.dao.CCNewMainMapper;
import com.etalk.crm.pojo.AchievementManagement;
import com.etalk.crm.pojo.NewMainBean;
import com.etalk.crm.service.CCNewMainService;
import com.etalk.crm.utils.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Jordan
 * @Auther: James
 * @Date: 2018/12/28 10:06
 * @Description: 课程顾问首页工作台
 */
@Service
public class CCNewMainServiceImpl implements CCNewMainService {
    @Resource
    private CCNewMainMapper ccNewMainMapper;

    @Override
    public PageInfo<NewMainBean> selectTrackRemindByUserId(Integer ccId, String searchDate, Integer sscId,Integer bccId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<NewMainBean> list=ccNewMainMapper.selectTrackRemindByUserId(ccId,searchDate,sscId,bccId);
        if (list!=null && list.size()>0){
            for (NewMainBean newMainBean : list) {
                if (newMainBean.getReleaseTime().getTime() > DateUtil.getCurrentDatetime().getTime()) {
                    newMainBean.setExpired(0);
                } else {
                    newMainBean.setExpired(1);
                }
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<NewMainBean> searchStuOrderInfoByCC(int ccId, Integer sscId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ccNewMainMapper.searchStuOrderInfoByUserId(ccId,sscId));
    }

    @Override
    public PageInfo<NewMainBean> demoMissedListByCC(int ccId, String searchDate, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ccNewMainMapper.demoMissedListByCC(ccId,searchDate));
    }

    @Override
    public PageInfo<NewMainBean> selectMainTipPotentialByCCId(int ccId,Integer state, Integer page, Integer size) {
        List<NewMainBean> list;
        Integer revisitId=0;
        if (state!=6){
            revisitId=ccNewMainMapper.selectParentIdById(state);
        }
        if (state==6 || revisitId==6){
            PageHelper.startPage(page, size);
            //查询已上可跟进(包含子类)的数据
            list=ccNewMainMapper.selectMainTipPotentialDemoTimeByCcId(ccId,state);
        }else {
            PageHelper.startPage(page, size);
            //查询非已上可跟进的数据
            list=ccNewMainMapper.selectMainTipPotentialByCCId(ccId,state);
        }
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<NewMainBean> selectMainTipRelLeadsByCC(int ccId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ccNewMainMapper.selectMainTipRelLeadsByCC(ccId));
    }

    @Override
    public PageInfo<NewMainBean> selectMainTipNewStudentByCCId(int ccId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ccNewMainMapper.selectMainTipNewStudentByCCId(ccId));
    }

    @Override
    public PageInfo<NewMainBean> selectOcInfoByCC(int ccId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ccNewMainMapper.selectOcInfoByCC(ccId));
    }

    @Override
    public PageInfo<NewMainBean> selectTeacherRank(int ccId, String searchKeys, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<String> listSearchKey=new ArrayList<>();
        if (StringUtils.isNotEmpty(searchKeys)){
            searchKeys=searchKeys.trim();
            listSearchKey.addAll(Arrays.asList(searchKeys.split(" ")));
        }
        return new PageInfo<>(ccNewMainMapper.selectTeacherRank(ccId,listSearchKey));
    }

    @Override
    public PageInfo<NewMainBean> selectLessonState(int ccId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ccNewMainMapper.selectLessonState(ccId));
    }

    /**
     * 学管师学员上完中教课提醒家长会课程
     * @param ccId cc id
     * @param finishId 是否完成家长会
     * @param page 页码
     * @param size 页记录数
     * @return
     */
    @Override
    public PageInfo<NewMainBean> searchBookingLctClassByCcId(int ccId, Integer finishId,Integer yearMonth,Integer page, Integer size) {
        PageHelper.startPage(page, size);
        if (yearMonth==null){
            yearMonth=0;
        }
        return new PageInfo<>(ccNewMainMapper.selectBookingLctClassByCcId(ccId,finishId,yearMonth));
    }

    /**
     * 查询未设置动态提醒的学员
     * @param ccId cc id
     * @param page 页码
     * @param size 页记录
     * @return
     */
    @Override
    public PageInfo<NewMainBean> searchNoReminderTimeStudentList(int ccId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ccNewMainMapper.selectNoReminderTimeStudentList(ccId));
    }

    /**
     * 查询订单总剩余课时少于40节的学员名单
     * @param ccId cc id
     * @param page 页码
     * @param size 页记录
     * @return
     */
    @Override
    public PageInfo<NewMainBean> searchSurplusClassLessNUmList(int ccId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ccNewMainMapper.selectSurplusClassLessNUmList(ccId));
    }

    /**
     * 老学员跟进
     * @param ccId cc id
     * @param page 页码
     * @param size 页记录数
     * @return
     */
    @Override
    public PageInfo<NewMainBean> searchOcOldStudent(int ccId,Integer followDays, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ccNewMainMapper.selectOcOldStudent(ccId,followDays));
    }

    /**
     * 获得自动约课失败记录
     * @param ccId cc id
     * @param page 页码
     * @param size 记录数
     * @return
     */
    @Override
    public PageInfo<NewMainBean> searchAutoBookedFailList(int ccId, Integer page, Integer size){
        PageHelper.startPage(page, size);
        List<NewMainBean> list=ccNewMainMapper.selectAutoBookedFailList(ccId);
        for (NewMainBean newMainBean:list){
            JSONObject obj= JSON.parseObject(newMainBean.getRemark());
            newMainBean.setRemark(obj.getString("msg"));
        }
        return new PageInfo<>(list);
    }

    /**
     * 学员上课表现3星及以下列表
     * @param ccId cc id
     * @param page 页码
     * @param size 记录数
     * @return
     */
    @Override
    public PageInfo<NewMainBean> searchClassAttitudeList(int ccId, Integer page, Integer size){
        PageHelper.startPage(page, size);
        List<NewMainBean> list=ccNewMainMapper.selectClassAttitudeList(ccId);

        return new PageInfo<>(list);
    }

    /**
     * 学员上周、本周、下周课程数少于3节记录
     * @param ccId cc id
     * @param weekId 上周(-1),本周(0),下周(1)
     * @param page 页码
     * @param size 记录数
     * @return
     */
    @Override
    public PageInfo<NewMainBean> searchWeekClassInfoList(int ccId,Integer weekId, Integer page, Integer size){
        PageHelper.startPage(page, size);
        List<NewMainBean> list=ccNewMainMapper.selectWeekClassInfoList(ccId,weekId);

        return new PageInfo<>(list);
    }

    /**
     * cc名下学员当天课程
     * @param ccId cc id
     * @param page 页码
     * @param size 记录数
     * @return
     */
    @Override
    public PageInfo<NewMainBean> searchBookedCourseTodayList(int ccId, Integer classState,Integer page, Integer size){
        PageHelper.startPage(page, size);
        List<NewMainBean> list=ccNewMainMapper.selectBookedCourseTodayList(ccId,classState);

        return new PageInfo<>(list);
    }

    /**
     * 查询cc名下学员最近两次成绩
     * @param ccId cc id
     * @param page 页码
     * @param size 页记录数
     * @return
     */
    @Override
    public PageInfo<NewMainBean> searchLastTwoScoreList(int ccId, Integer page, Integer size){

        AchievementManagement record=ccNewMainMapper.selectNewScoreInfo();
        if (record==null){
            return new PageInfo<>();
        }
        PageHelper.startPage(page, size);
        List<NewMainBean> list=ccNewMainMapper.selectLastTwoScoreList(record.getIntYear(),record.getTerm(),record.getTestTime(),ccId);
        if (list!=null && list.size()>0){
            for (NewMainBean result:list){
                if (result.getScore2()!=null && result.getScore2().compareTo(BigDecimal.ZERO) > 0){
                    result.setDiffScore(result.getScore1().subtract(result.getScore2()));
                }
            }
            return new PageInfo<>(list);
        }
        return new PageInfo<>();
    }
}
